<?php

namespace Tools\Utils;

use think\Config;

class Sms
{

    //发送手机验证码
    function sendsmscode($to)
    {
        $yzm = mt_rand(1000, 9999);
        $msg = new \Admin\Model\MessageModel();
        $status = $msg->send_smsMessage_register($to, 'sms_common', $yzm);//modified by yqp,将 sms_register 改为 sms_common
        if ($status) {
            SessionUtil::save("sms_register_sendcode" . $to, ["yzm" => $yzm, "expire_time" => time() + 7200]);

            return ["code" => 0, "msg" => '验证码已发送,请注意查收'];
        } else {
            return ["code" => 104, "msg" => '验证码发送失败,请确保你的手机号码正确.'];
        }
    }

    public function __construct($platform = '')
    {
        $this->platform = $platform ?? config('cunzan.sms');
    }

    /**
     * 发送短信验证码
     * @param $uid
     * @param $phone
     * @return array
     */
    protected function sendCode(int $phone, int $uid = 0)
    {
        if (env('APP_DEBUG')) {
            $redis = RedisStore::instanceDataCache();
            $redis_key = $this->phone_msg_redis_key . $uid . "_" . $phone;
            $redis->set($redis_key, 1234, 300);
            return true;
        }

        $counter = new Counter();
        $dayRegisterCount = $counter->dayCounterQuery(CounterConfig::DAY_MSG_SEND_ID, $uid);
        if ($dayRegisterCount >= CounterConfig::DAY_MSG_SEND_LIMIT) {
            return [
                'code' => '-1',
                'msg' => '每位用户每天最多仅允许发送' . CounterConfig::DAY_MSG_SEND_LIMIT . '次验证码！'
            ];
        }

        $lockmodel = RedisStore::instanceRedisLock();//加锁控制器
        $key = "sendcodemsg_" . $phone;//加锁键
        $lock = $lockmodel->set($key, 1, 60);//加锁
        $code = mt_rand(1000, 9999);
        if ($lock) {
            $msg = new \Api\Model\MessageModel();
            $status = $msg->send_sms_Message($phone, 'sms_common', $code, false);//modified by yqp,将 sms_register 改为 sms_common

            if ($status) {
                $redis = RedisStore::instanceDataCache();
                $redis_key = $this->phone_msg_redis_key . $uid . "_" . $phone;
                $redis->set($redis_key, $code, 300);
                $ret['code'] = 0;
                $ret['msg'] = "发送成功";
            } else {
                $ret['code'] = -1;
                $ret['msg'] = "验证码发送失败";
                $lockmodel->set($key, 0, 0);
            }
        } else {
            return [
                "code" => -1,
                "msg" => '您获取验证码操作过于频繁，请稍后再试!'
            ];
        }
        if ($ret['code'] === 0) {
//            $counter->dayCounterAdd(CounterConfig::DAY_MSG_SEND_ID, CounterConfig::DAY_MSG_SEND_LIMIT, 1, $uid);
            return $this->wxAjax(20000, $ret['msg']);
        } else {
            return $this->wxAjax(40000, $ret['msg']);
        }
    }

    public function checkCode(int $phone, string $code, $uid = '')
    {
        if (!$uid || !$phone || !$code) {
            return ['code' => -1, 'msg' => "必填参数为空!"];
        }
        $redis = RedisStore::instanceDataCache();
        $redis_key = $this->phone_msg_redis_key . $uid . "_" . $phone;
        $has_msg_code = $redis->get($redis_key);
        if (!$has_msg_code) {
            return ['code' => -1, 'msg' => "验证码已失效!"];
        }

        if ($code == $has_msg_code) {
            return ['code' => 1, 'msg' => "校验通过!"];
        } else {
            return ['code' => -1, 'msg' => "验证码错误!"];
        }

    }
}
